package com.jindianjichi.subject.domain.handler.subject;

import com.jindianjichi.basic.entity.SubjectRadio;
import com.jindianjichi.basic.service.SubjectRadioService;
import com.jindianjichi.subject.common.enums.IsDeletedEnum;
import com.jindianjichi.subject.common.enums.SubjectInfoTypeEnum;
import com.jindianjichi.subject.domain.convert.RadioSubjectConverter;
import com.jindianjichi.subject.domain.entity.SubjectAnswerBo;
import com.jindianjichi.subject.domain.entity.SubjectInfoBo;
import com.jindianjichi.subject.domain.entity.SubjectOptionBo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@Component
@Slf4j
public class SubjectRadioHandler implements SubjectInfoHandler{
    @Resource
    SubjectRadioService subjectRadioService;
    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.RADIO;
    }

    @Override
    public void add(SubjectInfoBo subjectInfoBo) {
        List<SubjectRadio> list=new ArrayList<>();
        Long subjectId=subjectInfoBo.getId();
        List<SubjectAnswerBo> optionList = subjectInfoBo.getOptionList();
        for (int i = 0; i < optionList.size(); i++) {
            SubjectRadio subjectRadio = new SubjectRadio();
            subjectRadio.setSubjectId(subjectId);
            subjectRadio.setOptionType(optionList.get(i).getOptionType());
            subjectRadio.setIsCorrect(optionList.get(i).getIsCorrect());
            subjectRadio.setOptionContent(optionList.get(i).getOptionContent());
            subjectRadio.setIsDeleted(IsDeletedEnum.UN_DELETED.getCode());
            list.add(subjectRadio);
        }


        log.info("list的值是{}",list);

        subjectRadioService.insertRadios(list);
    }

    @Override
    public SubjectOptionBo queryById(Long subjectId) {
       SubjectRadio subjectRadio=new SubjectRadio();
        subjectRadio.setSubjectId(subjectId);
        List<SubjectRadio> Radiolist = subjectRadioService.queryByCondition(subjectRadio);
        List<SubjectAnswerBo> answerBos = RadioSubjectConverter.instance.convertAnswerBolist(Radiolist);
        SubjectOptionBo subjectOptionBo=new SubjectOptionBo();
        subjectOptionBo.setOptionList(answerBos);
        return subjectOptionBo;
    }
}
